#include <iostream>
#include <thread>
#include <string>

#include <boost/asio.hpp>
#include <boost/array.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/program_options.hpp>
#include <boost/asio/detail/socket_option.hpp>
#include <boost/system/system_error.hpp>
#include <boost/asio/thread_pool.hpp>
#include <boost/lexical_cast.hpp>

int main(){
    std::string local,remote, network;
    std::vector<std::string> locals,remotes;
    boost::program_options::options_description desc("Allowed options");
    desc.add_options()
        ("help,h", "Print help message")
        ("network,n", boost::program_options::value<std::string>(&network), "tcp or udp")
        ("local,l", boost::program_options::value<std::string>(&local), "local ip address")
        ("remote,r", boost::program_options::value<std::string>(&remote)->required(), "remote ip address");

    boost::asio::thread_pool pool(10);
    boost::split(locals,local,boost::is_any_of(","));
    boost::split(remotes,remote,boost::is_any_of(","));

    if(network=="tcp"){
        boost::asio::io_service io_service;
        boost::asio::ip::tcp::acceptor acceptor(io_service,
            boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), boost::lexical_cast<int>(locals[1])));
        while (true) {
            std::list<boost::asio::ip::tcp::socket> sockets;
            sockets.emplace_back(acceptor.accept());
            std::thread([](std::list<boost::asio::ip::tcp::socket> ss,boost::asio::ip::tcp::socket s){
                boost::array<char, 1024> data;
                while(true){
                    boost::system::error_code ignored_error;
                    size_t length = s.read_some(boost::asio::buffer(data),ignored_error);
                    if(ignored_error) ss.remove_if([&](auto& it){return s.remote_endpoint()==it.remote_endpoint();});
                    std::string message(data.data(), length);
                    std::string addr = s.remote_endpoint().address().to_string();
                    s.write_some(boost::asio::buffer(addr),ignored_error);
                    if(ignored_error) ss.remove_if([&](auto& it){return s.remote_endpoint()==it.remote_endpoint();});
                }
            }, std::move(sockets), std::move(sockets.back())).detach();
        }
    }else{
        boost::array<char, 1024> data;
        boost::asio::io_service io_service;
        boost::asio::ip::udp::socket socket(io_service, 
            boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), boost::lexical_cast<int>(locals[1])));
        while(true){
            boost::asio::ip::udp::endpoint recv_endpoint;
            size_t length = socket.receive_from(boost::asio::buffer(data), recv_endpoint);
            socket.send_to(boost::asio::buffer(recv_endpoint.address().to_string()),recv_endpoint);
        }
    }

    return 0;
}